function PeriodlistAssistant() {};

PeriodlistAssistant.prototype.setup = function() {
this.debugContainer = this.controller.get("debugOutput");
    this.logOutputNum = 0;
	// set up button	
	this.controller.setupWidget("btnSync", { type: Mojo.Widget.activityButton },
		    {
			label : "Synchronization",
			buttonClass : "buttonfloat affirmative",
			disabled:!islogin,
		    }
		  ); 
	Mojo.Event.listen(this.controller.get("btnSync"), Mojo.Event.tap, 
				    this.callGetPeriod.bind(this)
				  ); 
    //set up list				  
	this.listModel = {listTitle: username, items:getLocalData("perioditems")||[]};
		
	this.controller.setupWidget('listPeriods', 
				    {itemTemplate:'periodlist/listitem', listTemplate:'periodlist/listcontainer'},
				     this.listModel);

	Mojo.Event.listen(this.controller.get("listPeriods"), Mojo.Event.listTap,
                    this.listPeriodstap.bind(this));
					
    if(!getLocalData("perioditems")){
	    this.callGetPeriod();	
	}
};
PeriodlistAssistant.prototype.activate = function(event) {
};
PeriodlistAssistant.prototype.callGetPeriod = function() {
    var f = new Future();
    //Mojo.Log.info("configObject before trans " + JSON.stringify(configObject));

    var configObj = configObject;
	
	//callTransService(configObj, 'getPPMTimePeriod', {}, f);
	callTransService(configObj, 'getPPMTimesheetByResource', {"criteria(tsResource)":username}, f);
    
    f.then( function(future) {            
        //Mojo.Log.info("future result ", JSON.stringify(future.result));
		
		// this.listModel.items = future.result.reply;
		// setLocalData("perioditems",this.listModel.items)
		// this.controller.modelChanged(this.listModel);
		this.callGetNewPeriods(future.result.reply);
		this.controller.get("btnSync").mojo.deactivate();
		//this.logMsg( "code : " + future.result.code +", Service Return: " +JSON.stringify(future.result.reply)); 
    }.bind(this));

};
PeriodlistAssistant.prototype.callGetNewPeriods = function(oldPeriods) {
this.logMsg("first period:"+JSON.stringify(oldPeriods[0]));
    var f = new Future();

    var configObj = configObject;
	callTransService(configObj, 'getPPMTimePeriod', {}, f);
	//callTransService(configObj, 'getPPMTimesheetByResource', {"criteria(tsResource)":username}, f);
    
    f.then( function(future) {            
        //Mojo.Log.info("future result ", JSON.stringify(future.result));
		
		//this.listModel.items = future.result.reply;
		var newitems = future.result.reply;
		var myitems = [];
		var lastIndex = 0;
		for(var i=0; i<newitems.length;i++){
		    if(oldPeriods[0].Time_Period.indexOf(newitems[i].Period)>=0)
			lastIndex = i;
		};
		for(var j=lastIndex+4; j>lastIndex;j--){
		    var myperiod = newitems[j].Period;
			if(myperiod.indexOf(">")>=0){
			    myperiod = myperiod.replace(">","<b style='color:red;'>");
				myperiod+="</b>";
			}
		    myitems.push({Period:myperiod, SheetId:"",Action:"Create"});
		};
		for(var j=0; j<3;j++){
			var sheetId = oldPeriods[j].Time_Period_Link.substring(oldPeriods[j].Time_Period_Link.indexOf("timesheetId=")+12)
		    myitems.push({Period:oldPeriods[j].Time_Period, SheetId:sheetId,Action:"Edit"});			
		};
		this.listModel.items  = myitems;
		setLocalData("perioditems",this.listModel.items)
		this.controller.modelChanged(this.listModel);
		this.controller.get("btnSync").mojo.deactivate();
		this.logMsg( "code : " + future.result.code +", Service Return: " +JSON.stringify(myitems)); 
    }.bind(this));

};
PeriodlistAssistant.prototype.listPeriodstap = function(event){
    Mojo.Log.info("PeriodlistAssistant.listPeriodstap() entery! ");
	if(event.originalEvent.target.name === "btnEdit"){
	    if(event.item.Action == "Edit")
		Mojo.Controller.stageController.pushScene({name: "timesheet", disableSceneScroller: false},event.item);
	}
    
};
PeriodlistAssistant.prototype.callCreateTimesheet = function(event) {
    var f = new Future();
    //Mojo.Log.info("configObject before trans " + JSON.stringify(configObject));

    var configObj = configObject;
	
	var params = {
					    "periodId":"30246",
						"resource":username,
						"resourceHV":"",
						"description":"created by mobile for test",
						"includePrevWorkItems":"on",
						"lastTimeSheet":"2513245",
						"actionCreate":"Y",
						"copyTimeSheet":"false",
						"sourceTimeSheetId":"2513245"
	             };
				 
	callTransService(configObj, 'createTimesheetbyCopy',  params,  f);
	
	
	
	//callTransService(configObj, 'reworkPPMTimesheet', {}, f);
    //callTransService(configObj, 'getPPMTimesheetByResource', {"criteria(tsResource)":username}, f);
    //callTransServiceR2F(configObj, 'getRequestDetailsA', {"requestID":"RqAc_9849"}, this.controller,f);
    //callTransService(configObj, 'performPPMTimesheetApproval', {"tsSearchSelected%282414132_2%29":"on"}, f);
    
    f.then( function(future) {
        this.logMsg( "code : " + future.result.code +", Service Return: " +JSON.stringify(future.result.reply));     
        Mojo.Log.info(" future result ", JSON.stringify(future.result));
        this.controller.get("getRequestDetails").mojo.deactivate();
    }.bind(this));

};

PeriodlistAssistant.prototype.deactivate = function(event) {};

PeriodlistAssistant.prototype.cleanup = function(event) {};

PeriodlistAssistant.prototype.logMsg = function(logText) {
    //this.debugContainer.innerHTML = "" + (this.logOutputNum++) + ": " + StringUtils.escapeHTML(logText) + "<br/>" + this.debugContainer.innerHTML;
    //this.debugContainer.innerHTML = (this.logOutputNum++) + ": " + logText + "" + this.debugContainer.innerHTML;            
};  
